package com.yonyou.jrmob.contacts.schedule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.schedule.view.ToggleButton;
import com.yonyou.jrmob.contacts.schedule.vo.ScheduleDetail;
import com.yonyou.uap.um.service.ServiceCallback;

import org.json.JSONObject;

import jrmob.yonyou.com.commonlib.app_config_tool.constant.Constant;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.StatusBarUtil;
import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;
import jrmob.yonyou.com.commonlib.login.bean.MAServices_lib;

/*
 * 详情页面
 */
public class ScheduleDetailActivity extends BaseActivityMob_lib {


	private TextView kaishishijian;// 开始时间
	private TextView jieshushijian;// 结束时间
	private TextView topbar_title;// title
	//private Button cancleBtn;// button_left
	private Button delBtn;// button_right
	private ToggleButton switchs;
	private BaseActivityMob_lib context = null;
	private TextView tv_huiyishi11;
	private TextView tv_tixing;
	private TextView tv_theme;
	private TextView et_didian;
	private TextView et_beizhu;
	private TextView tv_faqiren;
	private ImageView iv_canyuren;
	private TextView tv_canyuren;
	private ScheduleDetail detail;
	//private GoogleApiClient client;
	//private List<ScheduleDetail> scheduleList;

	private String initStartDateTime = "2016年6月10日 14:44"; // 初始化开始时间
	private String initEndDateTime = "2016年6月11日 17:44"; // 初始化结束时间
	private ScheduleDetail ScheduleVO;
	private String pk;
	private ImageView top_img_back;

	//private ScheduleFragment scheduleFragment = new ScheduleFragment();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initActionbar();
		setContentView(R.layout.schedulemainactivity);
		context = this;
		initview();
		initListener();
		InitData();
	}

	@Override
	public void initView() {

	}

	private void InitData() {
		Intent intent = this.getIntent();
		detail=(ScheduleDetail) intent.getSerializableExtra("detail");
		if (!detail.getStartPerson().equals(getUserInfoLRY().getUser_name())){
			delBtn.setVisibility(View.INVISIBLE);
		}
		setValue(detail);
	}

	@Override
	protected void onResume() {
		super.onResume();
		StatusBarUtil.setStatusBarColor(this, R.color.schedule_status_bar_bg);
	}

	/***
	 * 调用后台服务
	 */
	private void deleteCallService(String pk) {

		JSONObject json = new JSONObject();
		try {
			json.put("transtype", Constant.TRANSTYPE_DELETE_CALENDAR);
			json.put("user_id", getUserInfoLRY().getUser_id());
			json.put("pk_calendar", pk);
			json.put("uuidToken", Constant.TOKEN);

			if (!CommonUtil.isNetworkConnected(ScheduleDetailActivity.this)) {
				CommonUtil.showToast(ScheduleDetailActivity.this, "当前网络不可用,请检查!");
				return;
			}
			MAServices_lib.callMACommonService(ScheduleDetailActivity.this, new ServiceCallback() {
				@Override
				public void execute(JSONObject json) {
					// 有错误的时候
					if ("0".equals(json.optString("code"))) {
						String msg = json.optString("message");
						showMessage(msg);
						return;
					}
				}
				@Override
				public void error(String arg0, String arg1) {
					// 设置登录按钮可用性(登录请求后台有反应后可用)
					Toast.makeText(ScheduleDetailActivity.this, arg0 + arg1,
							Toast.LENGTH_LONG).show();
				}
			}, json);
			Intent intent = new Intent(ScheduleDetailActivity.this, ScheduleActivity.class);
			setResult(55, intent); // 55 代表响应吗
			delBtn.postDelayed(new Runnable() {
				@Override
				public void run() {
					finish();
				}
			}, 500);

		} catch (Exception e) {
			// 设置登录按钮可用性(登录请求后台有反应后可用)
			Log.e("mmmm", e + "");
			e.printStackTrace();
		}
	}

	public void initListener() {

		top_img_back.setOnClickListener(this);
		delBtn.setOnClickListener(this);
		switchs.setToggleState(ToggleButton.ToggleState.Close);

		switchs.setToggleState(ToggleButton.ToggleState.Open);// 默认是开着的
		switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_on);
		/*switchs.setEnabled(false);
		switchs.setFocusable(false);*/
	}

	@Override
	public void initData() {

	}

	/**
	 * 组装界面信息显示
	 *
	 * @param vo
	 */
	public void setValue(final ScheduleDetail vo) {
		tv_theme.setText(vo.getTheme());
		kaishishijian.setText(vo.getStartTime());
		jieshushijian.setText(vo.getEndTime());
		if (vo.getIsZhongYao().equals("1")){
			switchs.setToggleState(ToggleButton.ToggleState.Open); // 是否重要
			switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_on);
		}else{
			switchs.setToggleState(ToggleButton.ToggleState.Close);
			switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_off);
		}
		switchs.setOnToggleStateChangeListener(new ToggleButton.OnToggleStateChangeListner() {
			@Override
			public void onToggleStateChange(ToggleButton.ToggleState state) {
				Toast.makeText(ScheduleDetailActivity.this,"操作无效！",Toast.LENGTH_SHORT).show();
				if (vo.getIsZhongYao().equals("1")){
					switchs.setToggleState(ToggleButton.ToggleState.Open); // 是否重要
					switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_on);
				}else{
					switchs.setToggleState(ToggleButton.ToggleState.Close);
					switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_off);
				}
			}
		});
		tv_tixing.setText(vo.getTiXing());
		tv_huiyishi11.setText(vo.getHuiYiShi());
		et_didian.setText(vo.getLocation());
		et_beizhu.setText(vo.getMemo());
		tv_faqiren.setText(vo.getStartPerson());
		tv_canyuren.setText(vo.getCanYuPerson());

	}

	private void initview() {
		/*cancleBtn = (Button) findViewById(R.id.cancel_btn);
		cancleBtn.setBackground(getResources().getDrawable(R.drawable.back_normal));*/
		delBtn = (Button) findViewById(R.id.add_btn);

		kaishishijian = (TextView) findViewById(R.id.kaishishijian);
		tv_theme = (TextView) findViewById(R.id.tv_theme);
		jieshushijian = (TextView) findViewById(R.id.jieshushijian);
		topbar_title = (TextView) findViewById(R.id.topbar_title);
		tv_tixing = (TextView) findViewById(R.id.tv_tixing);
		tv_canyuren = (TextView) findViewById(R.id.tv_canyuren);
		iv_canyuren = (ImageView) findViewById(R.id.iv_canyuren);
		et_didian = (TextView) findViewById(R.id.et_didian);
		et_beizhu = (TextView) findViewById(R.id.et_beizhu);
		tv_faqiren = (TextView) findViewById(R.id.tv_faqiren);
		tv_huiyishi11 = (TextView) findViewById(R.id.tv_huiyishi11);
		topbar_title.setText("日程详情");
		top_img_back = (ImageView) findViewById(R.id.top_img_back);

		et_didian.setHint("");
		et_beizhu.setHint("");
		tv_huiyishi11.setHint("");

		tv_theme.setEnabled(false);
		kaishishijian.setEnabled(false);
		jieshushijian.setEnabled(false);
		tv_huiyishi11.setEnabled(false);
		tv_tixing.setEnabled(false);
		et_didian.setEnabled(false);
		et_beizhu.setEnabled(false);
		iv_canyuren.setVisibility(View.GONE);

		// 是否重要开关
		switchs = (ToggleButton) findViewById(R.id.switchs);
		switchs.setSlideBackgroudResource(R.mipmap.switch_slide);
		switchs.setToggleState(ToggleButton.ToggleState.Close);
		switchs.setSwitchBackgroudResource(R.mipmap.switch_bg_off);

	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.top_img_back){
			finish();
		}else if(v.getId() == R.id.add_btn){
			deleteCallService(detail.getPk());
		}
	}

}
